<?php
namespace jy_lianfeeshop;
defined('BY_JYA') or exit('error');
/*
	分销插件会员类
	如果将分销商各项数据在每次数据变动时计算好并存入数据库。
	那么分别需要做
		在设置上下级关系时，修改新上级的 下线数量、分销商数量、订单数量、订单佣金数量等数据
		在变更上下级关系时，修改原、新上级的各项参数
		在发生订单时，修改上级的订单相关参数
		在用户成为分销商时，修改上级的分销商数量相关数据
*/
class member extends \plugin{
	/*
		核实分销商
	*/
	public function checkmember($mid=0,$isexi=false){
			$mid=$mid?:$_SESSION['uid'];
			$member=$this->get('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));			
			$page_setting=$this->plugin_setting('page_setting');
			$page_setting['tagent']=$page_setting['tagent']?:'分销商';
			if(!$member['mid']){
				$this->register(array('mid'=>$mid));
				$member=$this->get('member',array('mid'=>$mid));
			}
			if(!$member['is_agent']){
				$apply=$this->get('apply',array('mid'=>$_SESSION['uid']),array('id','status'));
				if($apply['id'] && $apply['status']==0){
					if($isexi){
						exi('申请正在审核请耐心等待','error','jy_lianfeeshop/apply/');
					}else{
						return false;
					}
				}
				$member['can_apply']=false;
				//如果成为分销商无条件，此处自动申请成为分销商
				if($this->setting['become']==0){
					//分销商无需审核，此处自动审核
					$member['can_apply']=true;
					$this->apply_agent($member['mid']);
					if($this->setting['isshenhe'] && $isexi){
						exi('申请正在审核请耐心等待','error','jy_lianfeeshop/apply/');
					}else{
						exi('','','jy_lianfeeshop/index/');
					}
				}elseif($this->setting['become']==3){
					if($this->setting['team_tiaojian']==0){
						//自购单笔金额
						$pindex = max(1, 1);
						$psize = 1;
						$params=array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'mid'=>$mid);
						$orderby='price desc';
						$total='';
						$orders=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,'','',$orderby);
						$buyfee=$orders[0]['price'];
						$buyfee=$buyfee?:0;
						$member['now']=$buyfee;
						$member['limit']=$this->setting['tiaojian0'];
						$member['unit']='元';
						if($buyfee>=$this->setting['tiaojian0']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid']);
							$ispass=true;
						}
					}elseif($this->setting['team_tiaojian']==1){
						//自购订单总金额
						$buyfee=pdo_getsum('jy_weishop_order',array('ispay'=>1,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'price');
						$buyfee=$buyfee?:0;
						$member['now']=$buyfee;
						$member['limit']=$this->setting['tiaojian1'];
						$member['unit']='元';
						if($buyfee>=$this->setting['tiaojian1']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid']);
							$ispass=true;
						}
					}elseif($this->setting['team_tiaojian']==2){
						//自购订单数
						$buynums=pdo_count('jy_weishop_order',array('ispay'=>1,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
						$member['now']=$buynums;
						$member['limit']=$this->setting['tiaojian2'];
						$member['unit']='个';
						if($buynums>=$this->setting['tiaojian2']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid']);
							$ispass=true;
						}
					}elseif($this->setting['team_tiaojian']==3){
						//获得总分销佣金
						$buynums=get_getsum('jy_lianfeeshop_order',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'commission_money');
						$member['now']=$buynums;
						$member['limit']=$this->setting['tiaojian3'];
						$member['unit']='元';
						if($buynums>=$this->setting['tiaojian3']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid']);
							$ispass=true;
						}
					}elseif($this->setting['team_tiaojian']==4){
						//下线总人数
						$buynums=pdo_count('jy_lianfeeshop_member',array('fromuser'=>$mid,'uniacid'=>$_SESSION['uniacid']));
						$member['now']=$buynums;
						$member['limit']=$this->setting['tiaojian4'];
						$member['unit']='人';
						if($buynums>=$this->setting['tiaojian4']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid']);
							$ispass=true;
						}
					}elseif($this->setting['team_tiaojian']==5){
						//一级下线总人数
						$buynums=pdo_count('jy_lianfeeshop_member',array('fromuser'=>$mid,'uniacid'=>$_SESSION['uniacid']));
						$member['now']=$buynums;
						$member['limit']=$this->setting['tiaojian5'];
						$member['unit']='人';
						if($buynums>=$this->setting['tiaojian5']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid']);
							$ispass=true;
						}
					}elseif($this->setting['team_tiaojian']==6){
						//下线分销商
						$buynums=pdo_count('jy_lianfeeshop_member',array('is_agent'=>1,'fromuser'=>$mid,'uniacid'=>$_SESSION['uniacid']));
						$member['now']=$buynums;
						$member['limit']=$this->setting['tiaojian6'];
						$member['unit']='个';
						if($buynums>=$this->setting['tiaojian6']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid']);
							$ispass=true;
						}
					}elseif($this->setting['team_tiaojian']==7){
						//一级分销商
						$buynums=pdo_count('jy_lianfeeshop_member',array('is_agent'=>1,'fromuser'=>$mid,'uniacid'=>$_SESSION['uniacid']));
						$member['now']=$buynums;
						$member['limit']=$this->setting['tiaojian7'];
						$member['unit']='个';
						if($buynums>=$this->setting['tiaojian7']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid']);
							$ispass=true;
						}
					}elseif($this->setting['team_tiaojian']==8){
						//分销订单总金额
						$buynums=get_getsum('jy_lianfeeshop_order',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'order_price');
						$member['now']=$buynums;
						$member['limit']=$this->setting['tiaojian8'];
						$member['unit']='元';
						if($buynums>=$this->setting['tiaojian8']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid']);
							$ispass=true;
						}
					}elseif($this->setting['team_tiaojian']==9){
						//分销订单总数
						$buynums=pdo_count('jy_lianfeeshop_order',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
						$member['now']=$buynums;
						$member['limit']=$this->setting['tiaojian9'];
						$member['unit']='个';
						if($buynums>=$this->setting['tiaojian9']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid']);
							$ispass=true;
						}
					}elseif($this->setting['team_tiaojian']==10){
						//一级分销订单总金额
						$members=$this->getall('member',array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>$mid),array('mid','fromuser'),'mid');
						$member_ids=array_keys($members);
						if($member_ids){
							$buynums=get_getsum('jy_lianfeeshop_order',array('mid'=>$member_ids,'uniacid'=>$_SESSION['uniacid']),'order_price');
						}
						$member['now']=$buynums?:0;
						$member['limit']=$this->setting['tiaojian10'];
						$member['unit']='元';
						if($buynums>=$this->setting['tiaojian10']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid']);
							$ispass=true;
						}
					}elseif($this->setting['team_tiaojian']==11){
						//一级分销订单总数
						$members=$this->getall('member',array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>$mid),array('mid','fromuser'),'mid');
						$member_ids=array_keys($members);
						if($member_ids){
							$buynums=pdo_count('jy_lianfeeshop_order',array('mid'=>$member_ids,'uniacid'=>$_SESSION['uniacid']));
						}
						$member['now']=$buynums?:0;
						$member['limit']=$this->setting['tiaojian11'];
						$member['unit']='个';
						if($buynums>=$this->setting['tiaojian11']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid']);
							$ispass=true;
						}
					}elseif($this->setting['team_tiaojian']==12){
						//购买指定商品
						$become_goods=$this->setting['become_goods'];
						foreach($become_goods as &$bgv){
							$goods=pdo_get('jy_weishop_goods',array('id'=>$bgv['id']),array('unit','weight','subtitle','salenum','price'));
							$bgv=array_merge($bgv,$goods);
							$isbuy=pdo_count('jy_weishop_order_goods',array('mid'=>$member['mid'],'goodsid'=>$bgv['id'],'ispay'=>1));
							if($isbuy>0){
								$member['can_apply']=true;
								$this->apply_agent($member['mid']);
								$ispass=true;
								break;
							}
						}
						$member['become_goods']=$become_goods;
					}
					if($this->setting['isshenhe'] && $isexi && $ispass){
						exi('申请正在审核请耐心等待','error','jy_lianfeeshop/apply/');
					}elseif($ispass){
						if($member['is_agent']){
							exi('','','jy_lianfeeshop/index/');
						}
					}
					if($isexi){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_lianfeeshop/apply/');
					}
				}elseif($this->setting['become']==2){
					$member['can_apply']=true;
					if($isexi){
						exi('你还不是'.$page_setting['tagent'].'，请前往申请！','error','jy_lianfeeshop/apply/');
					}
				}elseif($this->setting['become']==1){
					$member['desc']='暂时无法申请，只能由管理员后台指定';
				}
			}elseif($member['stop'] && $isexi){
				exi('你的'.$page_setting['tagent'].'身份已禁用！','error','jy_lianfeeshop/apply/');
			}elseif((!$member['realname'] || !$member['mobile']) && $isexi){
				if($this->setting['isinfo']==1){
					exi('请先完善信息','error','jy_lianfeeshop/info/');
				}else{
					
				}
			}
			return $member;
	}
	/*
		升级成团长
	*/
	public function up_agent($member=array(),$isexi=false){
		if($member['is_agent']){
			return false;
		}
		$apply=$this->get('apply',array('mid'=>$_SESSION['uid']),array('id','status'));
		if($apply['id'] && $apply['status']==0){
			if($isexi){
				exi('申请正在审核请耐心等待','error','jy_lianfeeshop/apply/');
			}else{
				return false;
			}
		}
		if($member['stop']){
			if($isexi){
				exi('你的身份已禁用！','error','jy_lianfeeshop/apply/');
			}
			return false;
		}
		$mid=$member['mid'];
		//如果成为分销商无条件，此处自动申请成为分销商
		if($this->setting['become']==0){
					//分销商无需审核，此处自动审核
					$member['can_apply']=true;
					$this->apply_agent($member['mid'],'',true);
					if($this->setting['isshenhe'] && $isexi){
						exi('申请正在审核请耐心等待','error','jy_lianfeeshop/apply/');
					}
					return false;
		}elseif($this->setting['become']==3){
			if($this->setting['team_tiaojian']==0){
						//自购单笔金额
						$pindex = max(1, 1);
						$psize = 1;
						$params=array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'mid'=>$mid);
						$orderby='price desc';
						$total='';
						$orders=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,'','',$orderby);
						$buyfee=$orders[0]['price'];
						$buyfee=$buyfee?:0;
						$member['now']=$buyfee;
						$member['limit']=$this->setting['tiaojian0'];
						$member['unit']='元';
						if($buyfee>=$this->setting['tiaojian0']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid'],'',true);
							$ispass=true;
						}
			}elseif($this->setting['team_tiaojian']==1){
						//自购订单总金额
						$buyfee=pdo_getsum('jy_weishop_order',array('ispay'=>1,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'price');
						$buyfee=$buyfee?:0;
						$member['now']=$buyfee;
						$member['limit']=$this->setting['tiaojian1'];
						$member['unit']='元';
						if($buyfee>=$this->setting['tiaojian1']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid'],'',true);
							$ispass=true;
						}
			}elseif($this->setting['team_tiaojian']==2){
						//自购订单数
						$buynums=pdo_count('jy_weishop_order',array('ispay'=>1,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
						$member['now']=$buynums;
						$member['limit']=$this->setting['tiaojian2'];
						$member['unit']='个';
						if($buynums>=$this->setting['tiaojian2']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid'],'',true);
							$ispass=true;
						}
			}elseif($this->setting['team_tiaojian']==3){
						//获得总分销佣金
						$buynums=get_getsum('jy_lianfeeshop_order',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'commission_money');
						$member['now']=$buynums;
						$member['limit']=$this->setting['tiaojian3'];
						$member['unit']='元';
						if($buynums>=$this->setting['tiaojian3']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid'],'',true);
							$ispass=true;
						}
			}elseif($this->setting['team_tiaojian']==4){
						//下线总人数
						$buynums=pdo_count('jy_lianfeeshop_member',array('fromuser'=>$mid,'uniacid'=>$_SESSION['uniacid']));
						$member['now']=$buynums;
						$member['limit']=$this->setting['tiaojian4'];
						$member['unit']='人';
						if($buynums>=$this->setting['tiaojian4']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid'],'',true);
							$ispass=true;
						}
			}elseif($this->setting['team_tiaojian']==5){
						//一级下线总人数
						$buynums=pdo_count('jy_lianfeeshop_member',array('fromuser'=>$mid,'uniacid'=>$_SESSION['uniacid']));
						$member['now']=$buynums;
						$member['limit']=$this->setting['tiaojian5'];
						$member['unit']='人';
						if($buynums>=$this->setting['tiaojian5']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid'],'',true);
							$ispass=true;
						}
			}elseif($this->setting['team_tiaojian']==6){
						//下线分销商
						$buynums=pdo_count('jy_lianfeeshop_member',array('is_agent'=>1,'fromuser'=>$mid,'uniacid'=>$_SESSION['uniacid']));
						$member['now']=$buynums;
						$member['limit']=$this->setting['tiaojian6'];
						$member['unit']='个';
						if($buynums>=$this->setting['tiaojian6']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid'],'',true);
							$ispass=true;
						}
			}elseif($this->setting['team_tiaojian']==7){
						//一级分销商
						$buynums=pdo_count('jy_lianfeeshop_member',array('is_agent'=>1,'fromuser'=>$mid,'uniacid'=>$_SESSION['uniacid']));
						$member['now']=$buynums;
						$member['limit']=$this->setting['tiaojian7'];
						$member['unit']='个';
						if($buynums>=$this->setting['tiaojian7']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid'],'',true);
							$ispass=true;
						}
			}elseif($this->setting['team_tiaojian']==8){
						//分销订单总金额
						$buynums=get_getsum('jy_lianfeeshop_order',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']),'order_price');
						$member['now']=$buynums;
						$member['limit']=$this->setting['tiaojian8'];
						$member['unit']='元';
						if($buynums>=$this->setting['tiaojian8']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid'],'',true);
							$ispass=true;
						}
			}elseif($this->setting['team_tiaojian']==9){
						//分销订单总数
						$buynums=pdo_count('jy_lianfeeshop_order',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
						$member['now']=$buynums;
						$member['limit']=$this->setting['tiaojian9'];
						$member['unit']='个';
						if($buynums>=$this->setting['tiaojian9']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid'],'',true);
							$ispass=true;
						}
			}elseif($this->setting['team_tiaojian']==10){
						//一级分销订单总金额
						$members=$this->getall('member',array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>$mid),array('mid','fromuser'),'mid');
						$member_ids=array_keys($members);
						if($member_ids){
							$buynums=get_getsum('jy_lianfeeshop_order',array('mid'=>$member_ids,'uniacid'=>$_SESSION['uniacid']),'order_price');
						}
						$member['now']=$buynums?:0;
						$member['limit']=$this->setting['tiaojian10'];
						$member['unit']='元';
						if($buynums>=$this->setting['tiaojian10']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid'],'',true);
							$ispass=true;
						}
			}elseif($this->setting['team_tiaojian']==11){
						//一级分销订单总数
						$members=$this->getall('member',array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>$mid),array('mid','fromuser'),'mid');
						$member_ids=array_keys($members);
						if($member_ids){
							$buynums=pdo_count('jy_lianfeeshop_order',array('mid'=>$member_ids,'uniacid'=>$_SESSION['uniacid']));
						}
						$member['now']=$buynums?:0;
						$member['limit']=$this->setting['tiaojian11'];
						$member['unit']='个';
						if($buynums>=$this->setting['tiaojian11']){
							$member['can_apply']=true;
							$this->apply_agent($member['mid'],'',true);
							$ispass=true;
						}
			}elseif($this->setting['team_tiaojian']==12){
						//购买指定商品
						$become_goods=$this->setting['become_goods'];
						foreach($become_goods as &$bgv){
							$goods=pdo_get('jy_weishop_goods',array('id'=>$bgv['id']),array('unit','weight','subtitle','salenum','price'));
							$bgv=array_merge($bgv,$goods);
							$isbuy=pdo_count('jy_weishop_order_goods',array('mid'=>$member['mid'],'goodsid'=>$bgv['id'],'ispay'=>1));
							if($isbuy>0){
								$member['can_apply']=true;
								$this->apply_agent($member['mid'],'',true);
								$ispass=true;
								break;
							}
						}
						$member['become_goods']=$become_goods;
			}
			if($this->setting['isshenhe'] && $isexi && $ispass){
						exi('申请正在审核请耐心等待','error','jy_lianfeeshop/apply/');
			}
			if($isexi){
						exi('你还不是分销商请前往申请！','error','jy_lianfeeshop/apply/');
			}
		}elseif($this->setting['become']==2){
					$member['can_apply']=true;
					if($isexi){
						exi('你还不是分销商，请前往申请！','error','jy_lianfeeshop/apply/');
					}
		}elseif($this->setting['become']==1){
					$member['desc']='暂时无法申请，只能由管理员后台指定';
		}
		return true;
	}
	public function member_update($mid,$update=array()){
		pdo_update('jy_lianfeeshop_member',$update,array('mid'=>$mid));
		return true;
	}
	/*
		会员注册
	*/
	public function register($member=array(),$isbuy=false){
		if(!$member['uniacid']){
			$member['uniacid']=$_SESSION['uniacid'];
		}
		if(!$member['mid']){
			$member['mid']=$_SESSION['uid'];
		}
		if(!$member['createtime']){
			$member['createtime']=TIMESTAMP;
		}
		if(!$member['mid']){
			return error('分销商会员注册失败，缺少会员ID！');
		}
        $ismid=$this->get('member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid']));
        if($ismid){
			if(!$ismid['agent_id'] && $this->setting['agent_id']){
				$agent_id=$this->setting['agent_id'];
				if($agent_id){
					$this->set_parent($member['mid'],$agent_id);
				}
			}
            return $ismid;
        }
		$member['fromuser']=$_SESSION['fromuser']?:pdo_getcolumn('core_members',array('id'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']),'fromuser');
		$member['fromuser']=$member['fromuser']==$member['mid']?0:$member['fromuser'];
		if($isbuy){
			$is_agent=$this->getcolumn('member',array('mid'=>$member['fromuser'],'uniacid'=>$_SESSION['uniacid']),'is_agent');
			if(!$is_agent){
				$member['fromuser']=0;
			}
		}
		if(!$member['fromuser'] && $this->setting['agent_id']){
			$member['fromuser']=$member['gei_mid']=$this->setting['agent_id'];
		}
		$member['fromuser']=intval($member['fromuser']);
		$this->insert('member',$member);
		$member['id']=pdo_insertid();
		return $member;
	}
	/*
		申请成为分销商
	*/
	public function apply_agent($mid,$area_code='',$isnoexi=false){
		$apply=$this->get('apply',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		if($apply['id'] && $apply['status']==1){
			$this->update('member',array('is_agent'=>1,'check_time'=>TIMESTAMP),array('mid'=>$mid,'is_agent'=>0));
			$fromuser=$this->getcolumn('member',array('mid'=>$apply['mid']),'fromuser');
			$this->update_agent_level($fromuser);
			if(!$isnoexi){
				exi('您的申请已经通过！','error','jy_lianfeeshop/member.index/');
			}
			return false;
		}elseif($apply['id'] && $apply['status']==0){
			if(!$isnoexi){
				exi('您已经申请了分销商！','error','jy_lianfeeshop/apply/');
			}
			return false;
		}
		if($apply['id']){
			$id=$apply['id'];
		}else{
			$new_apply=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'status'=>0,
				'createtime'=>TIMESTAMP,
			);
			$this->insert('apply',$new_apply);
			$id=$this->insertid();
		}
		if(!$this->setting['isshenhe']){
			//分销商无需审核，此处自动审核
			$this->check_agent($id,1);
		}
		return $id;
	}
	/*
		审核分销商
	*/
	public function check_agent($applyid,$status,$remark=''){
		if(!$applyid || !$status){
			return;
		}
		$apply=$this->get('apply',array('id'=>$applyid));
		if(!$apply){
			return;
		}
		$member=$this->get('member',array('mid'=>$apply['mid']));
		$fromuser=$member['fromuser'];
		$update=array(
			'status'=>$status,
			'complete_time'=>TIMESTAMP,
		);
		if($update['status']==1){
			//审核通过，如果会员还不是分销商，此处修改
			$this->update('member',array('is_agent'=>1,'check_time'=>TIMESTAMP),array('mid'=>$apply['mid'],'is_agent'=>0,'uniacid'=>$_SESSION['uniacid']));
			$this->update_agent_level($fromuser);
		}else{
			$update['remark']=$remark;
		}
		$this->update('apply',$update,array('id'=>$applyid));
		return true;
	}
	public function set_parent($mid,$fromuser=0,$isshou=false){
		$mid=$mid?:$_SESSION['uid'];
		if($mid==$fromuser || (!$fromuser && !$isshou)){
			return false;
		}
		//如果他的上级是我
		$ta_agent_id=$this->getcolumn('member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']),'fromuser');
		if($ta_agent_id==$mid){
			return true;
		}
		$member=$this->get('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		if($fromuser==$member['fromuser']){
			return false;
		}
		$this->update('member',array('fromuser'=>$fromuser,'updatetime'=>TIMESTAMP),array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		//上级获得抽奖机会
		$gamepost=array(
			'type'=>8,
			'level_id'=>'lianfeeshop_'.$member['level_id'],
		);
		if($fromuser){
			model('jy_weishop/game')->up_addgame($gamepost,$fromuser);
		}
		$this->update_agent_level($fromuser);
		return true;
	}
	/*
		禁用分销商
	*/
	public function stop_agent($agent_id){
		$this->update('member',array('stop'=>1),array('mid'=>$agent_id,'uniacid'=>$_SESSION['uniacid']));
	}
	/*
		恢复分销商
	*/
	public function start_agent($agent_id){
		$this->update('member',array('stop'=>0),array('mid'=>$agent_id,'uniacid'=>$_SESSION['uniacid']));
	}

	/*
		升级分销商等级
		先检测会员身份是否为正常分销商
		然后获取分销商当前等级的下一等级升级条件
		然后逐个条件对比，看是否满足升级条件
		如果全部满足，则升级到相应等级
		
		何时触发升级分销商等级检测呢？
		
	*/
	public function update_agent_level($mid,$self=false){
		if(!$mid){
			return;
		}
		$member=$this->get('member',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		if(!$member['mid']){
			return;
		}
		if($member['is_agent']!=1){
			$this->up_agent($member);
		}
		if($member['is_agent']!=1 && !$self){
			return;
		}
		if($member['stop'] && !$self){
			return;
		}
		$level=$this->get('level',array('id'=>$member['level_id']));
		$level['level']=$level['level']?:0;
		$next_level=pdo_get('jy_lianfeeshop_level',array('uniacid'=>$_SESSION['uniacid'],'level >'=>$level['level']),'','level asc');
		if(!$next_level){
			return;
		}
		$conditions=json_decode($next_level['conditions'],true);
		$level_up_types=$this->model('model')->current_level_up_types();//获取当前设置的分销商等级升级条件
		if(!$level_up_types){
			return;
		}
		$up=true;
		$levels=pdo_get('jy_lianfeeshop_level',array('uniacid'=>$_SESSION['uniacid'],'level <'=>$level['level']),'','level desc');
		$on_level_id=$levels['id']?:0;
		$members=$this->getall('member',array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>$member['mid']),array('mid','fromuser'),'mid');
		if($members){
			$member_ids=array_keys($members);
			$member_ids[]=$member['mid'];
		}else{
			$member_ids=$member['mid'];
		}
		foreach($level_up_types as $type){
			if(!$conditions[$type] && $type!='buy_goods'){
				continue;	
			}
			//file_put_contents(ROOT_D.'/1.txt',$type.'~~'.$next_level['goods_ids']."\n",FILE_APPEND);
			if($type=='buy_goods' && $next_level['goods_ids']){
					$goods_ids=json_decode($next_level['goods_ids'],true);
					foreach($goods_ids as $gv){
						    if($type=='buy_goods'){
								$isbuynum=pdo_getsum('jy_weishop_order_goods',array('ispay'=>1,'mid'=>$member['mid'],'goodsid'=>$gv['id']),'total');
								if($isbuynum>0){
									break;
								}
							}
					}
					if(!$isbuynum){
						$up=false;
						break;
					}
			}elseif($type=='team_num'){
				//直推次等级个数(同等级)
				$member['team_num']=pdo_count('jy_lianfeeshop_member',array('is_agent'=>1,'level_id >='=>$member['level_id'],'fromuser'=>$member['mid']));
				if($member['team_num']<$conditions['team_num']){
					$up=false;
					break;
				}
			}elseif($type=='zhu_days'){
				//N天内给上级的预先分销商都升级了
				if($conditions['zhu_days'] && $conditions['zhu_days']*24*3600+$member['gei_time']<TIMESTAMP){
					$up=false;
					break;
				}
				$member['gei_num']=pdo_count('jy_lianfeeshop_member',array('level_id >'=>0,'gei_mid >'=>0,'fromuser'=>$member['mid']));
				if($this->setting['zhu_num'] && $member['gei_num']<$this->setting['zhu_num']){
					$up=false;
					break;
				}
			}elseif($type=='level_num'){
				//指定等级,指定等级人数（人）
				if($conditions['level_id']){
					$member['level_num']=pdo_count('jy_lianfeeshop_member',array('level_id'=>$conditions['level_id'],'fromuser'=>$member['mid']));
				}
				if($member['level_num']<$conditions['level_num']){
					$up=false;
					break;
				}
			}elseif($type=='order_price'){
				//团订单总金额（元）,团订单总数（个）
				$member['order_price']=pdo_getsum('jy_lianfeeshop_order',array('mid'=>$member_ids),'order_price');
				if($member['order_price']<$conditions['order_price']){
					$up=false;
					break;
				}
			}elseif($type=='order_num'){
				//团订单总金额（元）,团订单总数（个）
				$member['order_num']=pdo_count('jy_lianfeeshop_order',array('mid'=>$member_ids));
				if($member['order_num']<$conditions['order_num']){
					$up=false;
					break;
				}
			}elseif($type=='self_order_num'){
				//自购订单总数,自购订单总金额
			
					$member['self_order_num']=pdo_count('jy_weishop_order',array('ispay'=>1,'mid'=>$member['mid']));
			
				if($member['self_order_num']<$conditions['self_order_num']){
					$up=false;
					break;
				}
			}elseif($type=='self_order_price'){
				//自购订单总数,自购订单总金额

					$member['self_order_price']=pdo_getsum('jy_weishop_order',array('ispay'=>1,'mid'=>$member['mid']),'price');
			
				if($member['self_order_price']<$conditions['self_order_price']){
					$up=false;
					break;
				}
			}elseif($type=='commission_money'){
				$member['commission_money']=pdo_getsum('jy_lianfeeshop_commission',array('mid'=>$member['mid']),'money');
				if($member['commission_money']<$conditions['commission_money']){
					$up=false;
					break;
				}
			}elseif($type=='gei_midnum'){
				$member['gei_midnum']=pdo_count('jy_lianfeeshop_member',array('uniacid'=>$_SESSION['uniacid'],'gei_mid'=>$member['mid']),'id');
				if($member['gei_midnum']<$conditions['gei_midnum']){
					$up=false;
					break;
				}
			}
		}
		if($up){
			$update=array(
				'level_id'=>$next_level['id'],
			);
			$this->update('member',$update,array('id'=>$member['id']));
			//上级获得抽奖机会
			$gamepost=array(
				'type'=>8,
				'level_id'=>'lianfeeshop_'.$next_level['id'],
			);
			if($member['fromuser']){
				model('jy_weishop/game')->up_addgame($gamepost,$member['fromuser']);
				$this->update_agent_level($member['fromuser']);
			}
		}
		return true;
	}
	//给上级，上级获得二晋一；可以获得分销商分佣；原上级不能获得分销商分佣；但是可以获得平级分佣
	//如果A不属于分销商，A发展的下限（成交时）将全部贡献给老板；这时候A不能获得任何奖励！
	public function gei_member($member){
		if($member['fromuser']){
			$agent=$this->get('member',array('mid'=>$member['fromuser'],'uniacid'=>$_SESSION['uniacid']));
		}
		if(!$member['isbuy'] && $member['fromuser']){
			$frank=$this->count('member',array('isbuy'=>1,'fromuser'=>$member['fromuser'],'uniacid'=>$_SESSION['uniacid']));
			$frank=$frank?:0;
			$update=array('isbuy'=>1,'frank'=>$frank+1);
			if(!$agent['is_agent'] && $this->setting['agent_id']){
				$update['fromuser']=$this->setting['agent_id'];
				$update['gei_mid']=$this->setting['agent_id'];
			}
			$this->update('member',$update,array('mid'=>$member['mid'],'uniacid'=>$_SESSION['uniacid']));
			if(!$agent['is_agent'] && $this->setting['agent_id']){
				return $this->setting['agent_id'];
			}
		}
		if($member['gei_mid']>0 || !$member['fromuser'] || !$this->setting['zhu_num']){
			return false;
		}
		$psize=$this->setting['zhu_num']-$agent['is_gei'];
		if(!$psize && $agent['is_agent']){
			return false;
		}
		$fromuser=$agent['gei_mid']>0?$agent['gei_mid']:$agent['fromuser'];
		$boss=$this->get('member',array('mid'=>$fromuser,'uniacid'=>$_SESSION['uniacid']));
		if($boss['is_agent']){
			$this->update('member',array('gei_mid'=>$fromuser),array('id'=>$member['id'],'uniacid'=>$_SESSION['uniacid']));
			$this->update('member',array('is_gei +='=>1,'gei_time'=>TIMESTAMP),array('id'=>$agent['id'],'uniacid'=>$_SESSION['uniacid']));
		}else{
			if($agent['gei_mid']>0){
				$this->update('member',array('gei_mid'=>0),array('id'=>$agent['id'],'uniacid'=>$_SESSION['uniacid']));
			}
			$this->update('member',array('gei_mid'=>0),array('id'=>$member['id'],'uniacid'=>$_SESSION['uniacid']));
			$fromuser=0;
		}
		return $fromuser;
	}
	public function get_mid($member){
		$fromuser=$member['fromuser'];
		if($member['gei_mid']>0){
			$member=$this->get('member',array('mid'=>$fromuser));
			$this->get_mid($member);
		}else{
			return $fromuser;
		}
	}
	public function check_cangetfee($mid=0,$type=5){
		$mid=$mid?:$_SESSION['uid'];
		if($type==5){
			for($i=0;$i<5;$i++){
				$res=$this->check_file($this->setting['daili_file'.$i],$i);
				if($res){
					return true;
				}
			}
		}else{
			for($i=0;$i<5;$i++){
				$res=$this->check_file($this->setting['ping_file'.$i],$i);
				if($res){
					return true;
				}
			}
		}
		return false;
	}
	public function check_file($file,$key=0){
		return false;
	}
	//能否再获得奖金
	public function check_canaddfee($mid=0,$type=5){
		$mid=$mid?:$_SESSION['uid'];
		if($type==5){
			if($this->setting['daili_jie']){
				//封顶时，满额了
				if($this->setting['daili_type']==1){
					$fee=$this->sum('commission',array('type'=>5,'mid'=>$mid,'status <='=>1),'money');
					if($fee>=$this->setting['daili_manfee']){
						return false;
					}
				}
			}else{
				$isgetfee=$this->check_isgetfee($mid,$type);
				if($isgetfee){
					return false;
				}
			}
		}
		if($type==6){
			if($this->setting['ping_jie']){
				//封顶时，满额了
				if($this->setting['ping_type']==1){
					$fee=$this->sum('commission',array('type'=>6,'mid'=>$mid,'status <='=>1),'money');
					if($fee>=$this->setting['ping_manfee'] && $this->setting['ping_manfee']){
						return false;
					}
				}
			}else{
				$isgetfee=$this->check_isgetfee($mid,$type);
				if($isgetfee){
					return false;
				}
			}
		}
		return true;
	}
	public function check_isgetfee($mid=0,$type=5){
		$mid=$mid?:$_SESSION['uid'];
		$params=array(
			'status'=>3,
			'fee_type'=>$type,
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
		);
		$balance=$this->get('balance',$params);
		if($balance['id']){
			return true;
		}else{
			return false;
		}
	}
}